class TreeNode(object):
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right


class Solution(object):
    def goodNodes(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        self.ans = 0

        def dfs(node, val):
            if node.val >= val:
                self.ans += 1
            if node.left:
                dfs(node.left, max(val, node.val))
            if node.right:
                dfs(node.right, max(val, node.val))

        dfs(root, -100000)

        return self.ans